package com.bistu.xiong.factory.persistence;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.bistu.xiong.common.app.Application;
import com.bistu.xiong.factory.model.api.account.AccountRspModel;
import com.bistu.xiong.factory.model.db.User;
import com.bistu.xiong.factory.model.db.User_Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;

/**
 * Created by XiongXiaoJun on 2017/12/18.
 */

public class Account {
    private static String USER_ID = "";
    private static String USER_TOKEN = "";
    private static User currentUser = null;
    /**
     * 将该userId、username保存到SharedPreferences中
     * @param account
     */
    public static void saveAccount(AccountRspModel account){
        SharedPreferences sp = Application.getInstance().getSharedPreferences("AccountInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        USER_ID = account.getUserId();
        USER_TOKEN = account.getToken();
        editor.putString("USER_ID",USER_ID)
                .putString("USER_TOKEN",USER_TOKEN)
                .apply();
    }

    public static void load(){
        SharedPreferences sp = Application.getInstance().getSharedPreferences("AccountInfo", Context.MODE_PRIVATE);
        Account.USER_ID = sp.getString("USER_ID","");
        Account.USER_TOKEN = sp.getString("USER_TOKEN","");
    }

    /**
     * 判断用户是否已经登录
     * @return
     */
    public static boolean isLogin(){
        if (USER_ID.equals("")||USER_TOKEN.equals("")){
            load();
        }
        return !(USER_ID.equals("")||USER_ID ==null||USER_TOKEN.equals("")||USER_TOKEN==null);
    }

    /**
     * 判断用户信息是否完整
     * @return
     */
    public static boolean isComplete(){
        //保证登录成功
        if(isLogin()){
            //判断信息是否完整
            User self = getCurrentUser();
            return !TextUtils.isEmpty(self.getDesc())
                    && !TextUtils.isEmpty(self.getPortrait())
                    && self.getSex()!=0;
        }
        //未登录直接fasle
        return false;
    }

    /**
     * 获取当前登录的用户信息
     * @return User
     */
    public static User getCurrentUser() {
        if (Account.currentUser==null){
            // 如果为null返回一个new的User，其次从数据库查询
            currentUser = TextUtils.isEmpty(USER_ID) ? new User() : SQLite.select()
                    .from(User.class)
                    .where(User_Table.id.eq(USER_ID))
                    .querySingle();
        }
        // 返回当前的User
        return currentUser;
    }

    /**
     * 获取当前登录用户的USER_ID
     * @return USER_ID
     */
    public static String getUserId() {
        return USER_ID;
    }

    /**
     * 获取当前登录用户的USER_TOKEN
     * @return USER_TOKEN
     */
    public static String getToken() {
        return USER_TOKEN;
    }

    public static void destroyTokenAndUserId(){
        USER_TOKEN = "";
        USER_ID = "";
        SharedPreferences sp = Application.getInstance().getSharedPreferences("AccountInfo", Context.MODE_PRIVATE);
        sp.edit().clear().commit();

    }
}
